home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 22 / Cream of the Crop 22.iso / program / asm32.zip / E32.ZIP / FLOADFAR.ASM < prev    next >
Assembly Source File  |  1995-11-07  |  2KB  |  117 lines

  1. ; FLOADFAR.ASM - Copyright (C) 1995 Douglas Herr
  2. ;  all rights reserved
  3.  
  4. ; loads disk file to RAM
  5. ; call with [EDX] pointing to ASCIIZ filename
  6. ;
  7. ; returns:
  8. ;  if CF = 1, AX = DOS error code
  9. ;  if CF = 0, EAX = bytes loaded
  10. ;             BX = base selector address
  11.  
  12. include    model.inc
  13.  
  14. public    fileloadfar
  15. extrn    fsize:near
  16. IFDEF    DPMI
  17. extrn    GetMem32:near
  18. ENDIF
  19.  
  20. handle        equ    [ebp-2]
  21. bytes        equ    [ebp-6]        ; 4 bytes
  22. base_seg    equ    [ebp-8]        ; selector
  23. temp_ptr    equ    [ebp-12]
  24.  
  25. include    codeseg.inc
  26.  
  27. fileloadfar    proc    near
  28.  
  29.     push    ecx
  30.     push    edx
  31.     push    ds
  32.     push    es
  33.     pushfd
  34.     enter    12,0
  35.  
  36. ; open the file
  37. ; [EDX] -> file name
  38.     mov    ax,3D00h    ; open file for read access
  39.     int    21h
  40.     jc    short f10    ; exit if error
  41.     mov    handle,ax    ; save file handle
  42.  
  43. ; get file size
  44. ; I only need this to calculate memory requirements
  45.     mov    ebx,eax        ; handle
  46.     call    fsize
  47.     jc    short f7    ; exit if problem: close the file
  48.     mov    bytes,eax    ;  else save file size for exit
  49.  
  50. ; allocate memory
  51.     mov    ecx,eax        ; bytes
  52. IFDEF    DPMI
  53.     call    GetMem32
  54. ELSE
  55.     sys    GetMem32    ; get new selector
  56. ENDIF
  57.     jc    short f7    ; exit if error: close the file
  58.     mov    base_seg,bx    ; save for exit
  59.     jecxz    f7
  60.     xor    edx,edx        ; load file at start of segment
  61.     mov    ecx,65532    ; maximum to read: dword aligned for speed
  62.     mov    bx,handle
  63.     mov    ds,base_seg
  64.  
  65. ; read from file to DOS
  66. f5:    mov    ah,3Fh        ; read file
  67.     int    21h        ; ret: AX = bytes read from file
  68.     jc    short f6    ; error exit: close file & de-allocate memory
  69.  
  70. ; update buffer address & get another piece
  71.     movzx    eax,ax
  72.     add    edx,eax        ; point to next part of buffer
  73.     cmp    eax,ecx        ; did I read 65532?
  74.     je    f5        ;  yup, probably more to read
  75.     clc            ;  CF = 0, no error
  76.     jmp    short f7    ; we done: close the file
  77.  
  78. ; read error: de-allocate DOS buffer
  79. f6:    push    eax        ; save error code
  80.     mov    ax,ss
  81.     mov    ds,ax
  82.     mov    es,base_seg    ; initial segment address of block
  83.     mov    ah,49h        ; release memory
  84.     int    21h
  85.     pop    eax        ; restore error code
  86.     stc            ; restore error flag
  87.  
  88. ; exit sequence
  89. ; close the file
  90. f7:    pushf            ; save error flag & code
  91.     push    eax
  92.     mov    ax,ss
  93.     mov    ds,ax
  94.     mov    ah,3Eh
  95.     int    21h
  96.     pop    eax        ; restore error code
  97.     popf            ; restore error flag
  98.     jc    short f10
  99.  
  100.     mov    eax,bytes
  101.     mov    bx,base_seg
  102. f10:
  103.     leave
  104.     rcl    ecx,1        ; capture CF
  105.     popfd            ; all other flags restored
  106.     bt    ecx,0        ; restore CF
  107.     pop    es
  108.     pop    ds
  109.     pop    edx
  110.     pop    ecx
  111.     ret
  112.  
  113. fileloadfar    endp
  114.  
  115. @curseg    ends
  116.     end
  117.